package com.wl156.activities.find;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.wl156.MyApp;
import com.wl156.lite.R;
import com.wl156.activities.Xinyongxingji;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.utils.Utils;

public class SpecialLine extends Activity implements OnClickListener {

	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;

	private TextView mXinyongScore;
	private Button mXinyongDetail;

	private long mSpecialLineId;
	private String userID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_special_line);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		mSpecialLineId = getIntent().getLongExtra("id", 0);

		final TextView name = (TextView) findViewById(R.id.name);
		final TextView daysTruckSchedule = (TextView) findViewById(R.id.daysTruckSchedule);
		final TextView daysDuring = (TextView) findViewById(R.id.daysDuring);
		final TextView from = (TextView) findViewById(R.id.from);
		final TextView to = (TextView) findViewById(R.id.to);
		final TextView fromContact = (TextView) findViewById(R.id.from_contact);
		final ImageButton fromContactDial = (ImageButton) findViewById(R.id.from_contact_dial);
		fromContactDial.setOnClickListener(this);
		final TextView toContact = (TextView) findViewById(R.id.to_contact);
		final ImageButton toContactDial = (ImageButton) findViewById(R.id.to_contact_dial);
		toContactDial.setOnClickListener(this);
		final TextView price1 = (TextView) findViewById(R.id.price1);
		final TextView price2 = (TextView) findViewById(R.id.price2);
		final TextView companyAddr = (TextView) findViewById(R.id.companyAddr);
		final TextView contactPhone = (TextView) findViewById(R.id.contactPhone);
		final TextView contact = (TextView) findViewById(R.id.contact);
		Button collect = (Button) findViewById(R.id.collect);
		collect.setOnClickListener(this);
		mXinyongScore = (TextView) findViewById(R.id.xinyongscore);
		mXinyongDetail = (Button) findViewById(R.id.xinyongdetail);
		mXinyongDetail.setOnClickListener(this);

		new AsyncTask<Void, Void, Object>() {
			ProgressDialog mDialog;

			@Override
			protected void onPostExecute(Object result) {
				// 03-01 21:52:53.906: D/(26859): result: anyType{string=1;
				// string=信禾广汕专线; string=广东广州; string=广东汕头; string=1; string=1;
				// string=100; string=anyType{}; string=13565478914;
				// string=66123; string=广东广州火车省站; string=张三; string=15344889977;
				// string=anyType{}; string=广东汕头火车站; string=李四;
				// string=14577889944; string=0777-8899446; string=张小华;
				// string=15699887755; string=66457; string=20; string=3; },
				// type: SoapObject

				/*
				 * 05-15 23:19:07.773: D/(21579): result: anyType{string=2825;
				 * string=荭靓诚信物流有限公司; string=广东珠海; string=香港香港; string=null;
				 * string=anyType{}; string=anyType{}; string=15372531608;
				 * string=林短; string=立新; string=李鑫博; string=15372531608;
				 * string=15372531608; string=香港; string=李鑫博;
				 * string=15372531608; string=15372531608; string=null;
				 * string=林短; string=二星级; string=0; string=1; string=1;
				 * string=二星级; string=0; }, type: SoapObject
				 */
				if (result instanceof SoapObject) {
					SoapObject so = (SoapObject) result;
					name.setText(Utils.propertyToShow(so.getProperty(1)));
					from.setText(Utils.propertyToShow(so.getProperty(2)));
					to.setText(Utils.propertyToShow(so.getProperty(3)));
					// daysDuring.setText(Utils.propertyToShow(so.getProperty(4)));
					// daysTruckSchedule.setText(Utils.propertyToShow(so
					// .getProperty(5)));
					companyAddr
							.setText(Utils.propertyToShow(so.getProperty(4)));
					price1.setText(Utils.propertyToShow(so.getProperty(5)));
					price2.setText(Utils.propertyToShow(so.getProperty(6)));
					fromContact
							.setText(Utils.propertyToShow(so.getProperty(10)));
					fromContactDial.setTag(Utils.propertyToShow(so
							.getProperty(11)));
					toContact.setText(Utils.propertyToShow(so.getProperty(14)));
					toContactDial.setTag(Utils.propertyToShow(so
							.getProperty(15)));
					contact.setText(Utils.propertyToShow(so.getProperty(17)));
					contactPhone
							.setText(Utils.propertyToShow(so.getProperty(7)));
					mXinyongScore.setText(Utils.propertyToShow(so
							.getProperty(23)));
					userID = Utils.propertyToShow(so.getProperty(24));
				} else {
					Toast.makeText(SpecialLine.this, "获取信息失败，请稍候再试",
							Toast.LENGTH_LONG).show();
				}
				mDialog.dismiss();
			};

			@Override
			protected void onPreExecute() {
				mDialog = ProgressDialog.show(SpecialLine.this, "", "加载中");
			};

			@Override
			protected Object doInBackground(Void... params) {
				try {
					return mWebServiceClient.commonFindSpecialLineDetail(
							mCertificate, mSpecialLineId);
				} catch (IOException e) {
					e.printStackTrace();
					return e;
				} catch (XmlPullParserException e) {
					e.printStackTrace();
					return e;
				}
			}
		}.execute();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.xinyongdetail: {
			Intent i = new Intent(this, Xinyongxingji.class);
			i.putExtra("userID", userID);
			startActivity(i);
		}
			break;
		case R.id.from_contact_dial:
		case R.id.to_contact_dial:
			String phone = v.getTag().toString();
			Intent i = new Intent(Intent.ACTION_DIAL);
			i.setData(Uri.parse("tel:" + phone));
			startActivity(i);
			break;
		case R.id.collect:
			new AsyncTask<Void, Void, Object>() {
				ProgressDialog mDialog;

				@Override
				protected void onPreExecute() {
					mDialog = ProgressDialog.show(SpecialLine.this, "", "加载中");
				};

				@Override
				protected void onPostExecute(Object result) {
					if (result instanceof SoapObject) {
						SoapObject so = (SoapObject) result;
						final String[] ids = new String[so.getPropertyCount()];
						String[] names = new String[so.getPropertyCount()];
						String[] counts = new String[so.getPropertyCount()];
						for (int i = 0; i < so.getPropertyCount(); i++) {
							SoapObject line = (SoapObject) so.getProperty(i);
							String id = line.getPropertyAsString(0);
							ids[i] = id;
							String name = line.getPropertyAsString(1);
							names[i] = name;
							String count = line.getPropertyAsString(2);
							counts[i] = count;
						}

						AlertDialog.Builder builer = new Builder(
								SpecialLine.this);
						builer.setItems(names,
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											final int which) {
										new AsyncTask<Void, Void, Object>() {
											ProgressDialog mDialog;

											@Override
											protected void onPreExecute() {
												mDialog = ProgressDialog.show(
														SpecialLine.this, "",
														"添加中");
											};

											@Override
											protected void onPostExecute(
													Object result) {
												if (result instanceof SoapObject) {
													SoapObject so = (SoapObject) result;
													Toast.makeText(
															SpecialLine.this,
															"收藏成功",
															Toast.LENGTH_LONG)
															.show();
												} else if (result instanceof SoapPrimitive) {
													Toast.makeText(
															SpecialLine.this,
															result.toString(),
															Toast.LENGTH_LONG)
															.show();
												} else {
													Toast.makeText(
															SpecialLine.this,
															"网络异常，添加失败，请稍候再试",
															Toast.LENGTH_LONG)
															.show();
												}
												mDialog.dismiss();
											};

											@Override
											protected Object doInBackground(
													Void... params) {
												try {
													return mWebServiceClient
															.commonStoreGroupAddSpecialLineToGroup(
																	mCertificate,
																	String.valueOf(mSpecialLineId),
																	ids[which]);
												} catch (IOException e) {
													return e;
												} catch (XmlPullParserException e) {
													return e;
												}
											}
										}.execute();
									}
								});
						builer.show();
					} else {
						Toast.makeText(SpecialLine.this, "联网失败，请稍候再试",
								Toast.LENGTH_LONG).show();
					}
					mDialog.dismiss();
				};

				@Override
				protected Object doInBackground(Void... params) {
					try {
						return mWebServiceClient
								.commonGetMyStoreGroups(mCertificate);
					} catch (IOException e) {
						return e;
					} catch (XmlPullParserException e) {
						return e;
					}
				}
			}.execute();
			break;
		}
	}
}
